package mil.af.amc.adpetracker.dao.hibernate;

import mil.af.amc.adpetracker.dao.AccountDao;
import mil.af.amc.adpetracker.model.Account;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.NonUniqueResultException;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

/**
 * Please Fill in this Javadoc
 *
 * @author TSgt Jason Ferguson
 */
@Repository("accountDao")
public class HibernateAccountDao extends HibernateGenericDao<Account> implements AccountDao {

    private static final Log log = LogFactory.getLog(HibernateAccountDao.class);

    public HibernateAccountDao() {
        super(Account.class);
    }

    @SuppressWarnings({"unchecked"})
    public Account getByUsername(String username) {

        Criteria accountCriteria = sessionFactory.getCurrentSession().createCriteria(Account.class);
        accountCriteria.add(Restrictions.eq("username", username));

        Account result = null;
        try {

        } catch (NonUniqueResultException ex) {
            log.debug("getByUsername() threw NonUniqueResultException", ex);
            return null;
        } catch (HibernateException ex) {
            log.debug("getByUsername() threw HibernateException", ex);
        }

        return result;

//        DetachedCriteria crit = DetachedCriteria.forClass(Account.class);
//        crit.add(Restrictions.eq("username", username));
//
//        List<Account> results = (List<Account>) getHibernateTemplate().findByCriteria(crit);
//
//        if (results.size() < 1) {
//            return null;
//        } else {
//            getHibernateTemplate().initialize(results);
//            return results.get(0);
//        }
    }
}
